﻿#include "CTableDictionaryEngine.h"
#include "IpcClient.h"
#include "config/CConfig.h"

CTableDictionaryEngine::CTableDictionaryEngine(){
    _history_size = 2;
}
CTableDictionaryEngine::~CTableDictionaryEngine(){}




void CTableDictionaryEngine::CollectWord(const wchar_t *input,DWORD_PTR input_size, CImeArray<CCandidateListItem> *pCandidateList){
    
   // IPC_LOG(L"CollectWord : " + std::wstring(input,input_size));

    if(CConfig::EnableHistory){
        //查询历史
        if(input_size == 1 && input[0] == 'z' && !_history.empty()){
            for(auto &str : _history){
                auto item = pCandidateList->Append();
                item->_ItemString.Set(str.data(),str.size());
            }
            return;
        }
    }
    
    do{
        //太大了
        // if(input_size > 50){
        //     break;
        // }
        
        _candidates.clear();
        IpcClient::GetCandidates(input,(int)input_size,_candidates);
        if(_candidates.empty()){
            break; 
        }

        for(auto &str : _candidates){
            auto item = pCandidateList->Append();
            item->_ItemString.Set(str.data(),str.size());
           // IPC_LOG(L"Candidates : " + str);
        }
        return;
    }while(0);

    
    auto item = pCandidateList->Append();
    item->_ItemString.Set(input,input_size);
}

void CTableDictionaryEngine::AddHistory(const WCHAR *word, DWORD_PTR dwLength){
    if(!CConfig::EnableHistory){
        return;
    }
    _history.push_front(std::wstring(word,dwLength));
    if(_history.size() > _history_size){
        _history.pop_back();
    }
}